﻿Imports System.IO
Public Class filebase
    'created by: Sum
    'created date:26/12/2011
    'description: key and value simple file base storage,
    'modified by: sum
    'modified date: 2/1/2011
    'desc: add mustExist option for get Value
    Private _filename As String = String.Empty
    Public Sub New(ByVal filename As String)
        _filename = filename
        'check file
        If Not File.Exists(_filename) Then
            Dim stream As FileStream = File.Create(_filename)
            stream.Close()
        End If
    End Sub


    Public Function add_value(ByVal key As String, ByVal value As String) As Integer

        If String.IsNullOrEmpty(key) Then
            Throw New ArgumentNullException("key")
        End If

        If String.IsNullOrEmpty(value) Then
            Throw New ArgumentNullException("value")
        End If

        Dim rev As Integer = -1
        Dim txtoutput = key & "::" & value & vbNewLine
        Try
            Using outfile As New StreamWriter(_filename, True)
                outfile.Write(txtoutput.ToString())
            End Using
        Catch ex As Exception
            Throw ex
        End Try
        Return rev
    End Function

    Public Function get_value(ByVal key As String) As String

        If String.IsNullOrEmpty(key) Then
            Throw New ArgumentNullException("key")
        End If

        Dim rev As String = String.Empty
        Dim TextLine As String = String.Empty
        'get properties
        Using objReader As New System.IO.StreamReader(_filename)
            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine()
                If TextLine.Contains(key) Then
                    rev = TextLine.Replace("::", String.Empty).Replace(key, String.Empty).Replace(vbCr, String.Empty)
                End If
            Loop
        End Using
        Return rev
    End Function

    Public Function get_value(ByVal key As String, ByVal mustExist As Boolean, ByVal defaultVal As String) As String

        If String.IsNullOrEmpty(key) Then
            Throw New ArgumentNullException("key")
        End If

        Dim rev As String = String.Empty
        Dim TextLine As String = String.Empty
        'get properties
        Using objReader As New System.IO.StreamReader(_filename)
            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine()
                If TextLine.Contains(key) Then
                    rev = TextLine.Replace("::", String.Empty).Replace(key, String.Empty).Replace(vbCr, String.Empty)
                    Return rev
                End If
            Loop
        End Using

        If mustExist Then
            If rev = String.Empty Then
                Throw New Exception(key & "does not exist in " & _filename)
            End If
        End If

        Return defaultVal
    End Function



    Public Function update_value(ByVal key As String, ByVal newValue As String) As Integer
        Dim rev As Integer = 1
        Dim TextLine As String = String.Empty
        Dim output As String = String.Empty
        Dim found As Integer = 0
        'get properties
        Try
            Using objReader As New System.IO.StreamReader(_filename)
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    If TextLine.Contains(key) Then

                        found = TextLine.IndexOf("::")
                        output &= TextLine.Replace(TextLine.Substring(found + 2).Trim(), newValue) & vbNewLine
                    Else
                        output &= TextLine & vbNewLine
                    End If
                Loop
            End Using
            'replace
            Using outfile As New StreamWriter(_filename, False)
                outfile.Write(output.ToString())
            End Using

        Catch ex As Exception
            Throw ex
        End Try

        Return rev
    End Function

    Public Function delete_value(ByVal key As String) As Integer
        Dim rev As Integer = 1
        Dim TextLine As String = String.Empty
        Dim output As String = String.Empty
        Dim found As Integer = 0
        'get properties
        Try
            Using objReader As New System.IO.StreamReader(_filename)
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    If Not TextLine.Contains(key) Then
                        output &= TextLine & vbNewLine
                    End If
                Loop
            End Using
            'replace
            Using outfile As New StreamWriter(_filename, False)
                outfile.Write(output.ToString())
            End Using

        Catch ex As Exception
            Throw ex
        End Try

        Return rev
    End Function

    Public Function delete_file() As Integer
        Try
            File.Delete(_filename)
        Catch ex As Exception
            Throw ex
        End Try
        Return 1
    End Function


End Class
